package me.zhengjie.modules.transfer.factory.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.finance.domain.FinanceAd01;
import me.zhengjie.modules.finance.service.FinanceAd01Service;
import me.zhengjie.modules.sequence.domain.Sequence;
import me.zhengjie.modules.sequence.service.SequenceService;
import me.zhengjie.modules.transfer.domain.TransferIc58;
import me.zhengjie.modules.transfer.factory.service.TransferPaymentService;
import me.zhengjie.modules.transfer.factory.service.TransferProcessService;
import me.zhengjie.modules.transfer.service.*;
import me.zhengjie.tool.DateUtil;
import me.zhengjie.tool.MyMap;
import me.zhengjie.tool.NPETool;
import me.zhengjie.tool.ValidateUtil;
import me.zhengjie.vo.Constant;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class TransferPaymentServiceImpl implements TransferPaymentService {

    private final TransferIc90Service transferIc90Service;
    private final TransferIc89Service transferIc89Service;
    private final TransferIc65Service transferIc65Service;
    private final TransferIc66Service transferIc66Service;
    private final TransferIc71Service transferIc71Service;
    private final TransferIc74Service transferIc74Service;
    private final TransferIc69Service transferIc69Service;
    private final TransferIc60Service transferIc60Service;

    private final TransferIc58Service transferIc58Service;
    private final FinanceAd01Service financeAd01Service;
    private final SequenceService sequenceService;
    private final TransferProcessService transferProcessService;

    /**
     * 转出人员支付表生成
     *
     * @param businessParam
     * @return
     */
    @Override
    public Map transferoutCreatBills(Map businessParam) {

        businessParam.get("aaz002");
        businessParam.get("aae803");
        NPETool.isEmptyThrowException(businessParam.get("aaz002"),"业务日志ID");
        NPETool.isEmptyThrowException(businessParam.get("aae803"),"业务环节编码");
        String aaz002 = businessParam.get("aaz002").toString();
        String aae803 = businessParam.get("aae803").toString();
        List<Map> lxhList = null;
        List<Map> infoList = null;
        String aae140 = null;
        String bae038 = null;

        if (Constant.AAE803_T0102_ZYJXXXB.equals(aae803)
                || Constant.AAE803_T0502_ZRDFQDLSZHZ.equals(aae803)) {
            aae140 = Constant.AAE140_110_QYZGJBYLBX;

            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("aaz002", aaz002);
            lxhList = transferIc89Service.listMaps(queryWrapper);
            infoList = transferIc90Service.listMaps(queryWrapper);

            bae038 = "IC90";
        } else if (Constant.AAE803_T0703_CXJMYLBXZRCZZGYLBXXXB.equals(aae803)) {
            aae140 = Constant.AAE140_170_CXJMJBYLBX;


            bae038 = "IC72";
        } else if (Constant.AAE803_T0802_CZZGYLBXZRCXJMYLBXXXB.equals(aae803)) {
            aae140 = Constant.AAE140_110_QYZGJBYLBX;
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("aaz002", aaz002);
            lxhList = transferIc71Service.listMaps(queryWrapper);
            infoList = transferIc74Service.listMaps(queryWrapper);


            bae038 = "IC74";
        } else if (Constant.AAE803_T0902_CXJBYLBXKDQZYSPB.equals(aae803)) {
            aae140 = Constant.AAE140_170_CXJMJBYLBX;


            bae038 = "IC77";
        } else if (Constant.AAE803_T1102_JGYLBXZYXXB.equals(aae803) || Constant.AAE803_T1201_JGSYYLBXCZCTRYZYXXB.equals(aae803)) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("aaz002", aaz002);
            lxhList = transferIc65Service.listMaps(queryWrapper);
            infoList = transferIc66Service.listMaps(queryWrapper);
            bae038 = "IC66";
        } else if (Constant.AAE803_T3103_SYZYXXB.equals(aae803)) {

            bae038 = "JC90";
        }else if (Constant.AAE803_T1302_JGSYYLBXZYNJZYXXB.equals(aae803)){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("aaz002", aaz002);
            lxhList = transferIc69Service.listMaps(queryWrapper);
            infoList = transferIc60Service.listMaps(queryWrapper);

            bae038 = "IC60";
        }


        if (ValidateUtil.isEmpty(lxhList) || lxhList.size() > 1) {
            throw new IllegalArgumentException("创建支付单据，未获取到联系函信息");
        }
        if (ValidateUtil.isEmpty(infoList) || infoList.size() > 1) {
            throw new IllegalArgumentException("创建支付单据，未获取到信息表信息");
        }
        MyMap lxhMap = new MyMap(lxhList.get(0));
        MyMap infoMap = new MyMap(infoList.get(0));
        String aac001 = infoMap.getAsString("aac001");
        String aaz341;

        if (ValidateUtil.isEmpty(aae140)) {
            aae140 = infoMap.getAsString("aae140");
        }
        BigDecimal aic102;
        BigDecimal aic083 = BigDecimal.ZERO;
        BigDecimal aic084 = BigDecimal.ZERO;
        String aab004 = null;

        String aaa039 = Constant.AAA039_2_ZC;    //收支类型:VARCHAR2(1)
        String aae710 = Constant.AAE710_7002_ZYZC;    //业务处理类别:VARCHAR2(4)
        String bae710 = "";    //本地业务处理类别:VARCHAR2(8)

        if (Constant.AAE803_T0102_ZYJXXXB.equals(aae803)
                || Constant.AAE803_T0502_ZRDFQDLSZHZ.equals(aae803)) {
            aic102 = infoMap.getAsBigDecimal("aic102");
            aic083 = infoMap.getAsBigDecimal("aic083");
            aic084 = infoMap.getAsBigDecimal("aic084");
            aab004 = infoMap.getAsString("aab004");
            aaz341 = infoMap.getAsString("aaz341");
            bae710 = Constant.BAE710_700201_YL;
        } else if (Constant.AAE803_T0703_CXJMYLBXZRCZZGYLBXXXB.equals(aae803)) {
            aic102 = infoMap.getAsBigDecimal("aic083");
            aic083 = infoMap.getAsBigDecimal("aic083");
            aaz341 = infoMap.getAsString("aaz341");
            bae710 = Constant.BAE710_700201_YL;
        } else if (Constant.AAE803_T0802_CZZGYLBXZRCXJMYLBXXXB.equals(aae803)) {
            aic102 = infoMap.getAsBigDecimal("aic083");
            aic083 = infoMap.getAsBigDecimal("aic083");
            aaz341 = infoMap.getAsString("aaz341");
            bae710 = Constant.BAE710_700201_YL;
        } else if (Constant.AAE803_T0902_CXJBYLBXKDQZYSPB.equals(aae803)) {
            aic102 = infoMap.getAsBigDecimal("aic083");
            aaz341 = infoMap.getAsString("aaz341");
            bae710 = Constant.BAE710_700201_YL;
        } else if (Constant.AAE803_T1102_JGYLBXZYXXB.equals(aae803) || Constant.AAE803_T1201_JGSYYLBXCZCTRYZYXXB.equals(aae803)) {
            aic102 = infoMap.getAsBigDecimal("aic102");
            aic083 = infoMap.getAsBigDecimal("aic083");
            aic084 = infoMap.getAsBigDecimal("aic084");
            aab004 = infoMap.getAsString("aab004");
            aaz341 = infoMap.getAsString("aaz341");
            bae710 = Constant.BAE710_700201_YL;
        } else if (Constant.AAE803_T3103_SYZYXXB.equals(aae803)) {
            aic083 = infoMap.getAsBigDecimal("ajc079");
            aic102 = infoMap.getAsBigDecimal("ajc079");
            aaz341 = infoMap.getAsString("aaz341");
            bae710 = Constant.BAE710_700201_YL;
        }  else if (Constant.AAE803_T1302_JGSYYLBXZYNJZYXXB.equals(aae803)) {
            aic102 = infoMap.getAsBigDecimal("aie074");
            aaz341 = infoMap.getAsString("aaz341");
            bae710 = Constant.BAE710_700202_NJ;
        }else {
            throw new IllegalArgumentException("暂不支持的信息表类型！");
        }
        String aab301 = infoMap.getAsString("aab301");
        NPETool.isEmptyThrowException(aab301, "转出地行政区划");
        String aab299 = infoMap.getAsString("aab299");
        NPETool.isEmptyThrowException(aab299, "转入地行政区划");


        Sequence sequence_baz244 = new Sequence();
        sequence_baz244.setName("baz244");
        String aae072 = sequenceService.nextSequence(sequence_baz244);

        Sequence sequence_aae072 = new Sequence();
        sequence_aae072.setName("aae072");
        String baz244 = sequenceService.nextSequence(sequence_aae072);
        if (BigDecimal.ZERO.compareTo(aic102) < 0) {
            String aac002 = infoMap.getAsString("aac002");
            String aac003 = infoMap.getAsString("aac003");

            String aae002 = DateUtil.getCurrentTime("yyyyMM");



            String aaa028 = Constant.AAA028_11_DW;    //当事人类别:VARCHAR2(3)
            String aaz010 = aac001;    //当事人ID:NUMBER(20,0)
            String aae734 = aac003;    //当事人名称:VARCHAR2(450)
            String aac147 = aac002;    //证件号码:VARCHAR2(22)
            String aae718 = Constant.AAE718_1_PLZF;    //基金收支方式:VARCHAR2(1)
            String aaf200 = infoMap.getAsString("aaf200");    //银行行别代码:VARCHAR2(3)
            String aaf216 = infoMap.getAsString("aaf216");    //地方银行行别代码:VARCHAR2(8)
            String aae145 = Constant.AAE145_41_ZJDJGBF;    //发放方式String
            String aae711 = Constant.AAE711_0_WJS;    //数据状态:VARCHAR2(1)
            String aae825 = Constant.AAE825_0_F;    //锁定标志:VARCHAR2(1)
            String bad006 = Constant.BAD006_01_WCL;    //财务处理标志:VARCHAR2(2)
            String aae456 = Constant.AAE456_0_F;    //到账成功标志:VARCHAR2(1)

            String aae668 = deleteSpaceAndSpecialSpace(lxhMap.getAsString("aac155"));    //银行名称:VARCHAR2(150)
            String aae009 = deleteSpaceAndSpecialSpace(lxhMap.getAsString("aae009"));    //银行户名:VARCHAR2(150)
            String aae010 = deleteSpaceAndSpecialSpace(lxhMap.getAsString("aac078"));    //银行账号:VARCHAR2(40)
            String aaf002 = deleteSpaceAndSpecialSpace(lxhMap.getAsString("aaf002"));
            BigDecimal aae719 = aic102;

            String aaf217 = infoMap.getAsString("aaf217");
            String aad128 = aac003 + "_" + aac002 + "_转移支出" + aic102 + "元";


            String aae011 = infoMap.getAsString("aae011");    //经办人:VARCHAR2(100)
            String aaz692 = infoMap.getAsString("aaz692");    //经办人登录ID:VARCHAR2(50)
            String aab359 = infoMap.getAsString("aab359");    //经办机构
            String aae036 = DateUtil.getCurrentTime("yyyyMMddHHmmss");    //经办时间:NUMBER(14,0)
            String aac58 = Constant.AAC058_01_JMSFZ;

            TransferIc58 transferIc58 = new TransferIc58();

            transferIc58.setBaz244(baz244);
            transferIc58.setAae140(aae140);
            transferIc58.setAae002(new BigDecimal(aae002));
            transferIc58.setAaa039(aaa039);
            transferIc58.setAae710(aae710);
            transferIc58.setAae710(aae710);
            transferIc58.setAae719(aae719);
            transferIc58.setAae072(aae072);
            transferIc58.setBae038(bae038);
            transferIc58.setAaa028(aaa028);
            transferIc58.setAaz010(aaz010);
            transferIc58.setAae734(aae734);
            transferIc58.setAae718(aae718);
            transferIc58.setAaf200(aaf200);
            transferIc58.setAaf216(aaf216);
            transferIc58.setAaf217(aaf217);
            transferIc58.setAae668(aae668);
            transferIc58.setAae009(aae009);
            transferIc58.setAae010(aae010);
            transferIc58.setAab362("");
            transferIc58.setAaf056("");
            transferIc58.setAaf002(aaf002);
            transferIc58.setAad128(aad128);
            transferIc58.setAaz749(aae072);
            transferIc58.setAad080(aae140);
            transferIc58.setAae013("");
            transferIc58.setAae011(aae011);
            transferIc58.setAae036(new BigDecimal(aae036));
            transferIc58.setAab359(aab359);
            transferIc58.setAic083(aic083);
            transferIc58.setAic084(aic084);
            transferIc58.setAaz341(aaz341);
            transferIc58.setAaz002(aaz002);

            FinanceAd01 financeAd01 = new FinanceAd01();
            financeAd01.setBaz244(new BigDecimal(baz244));
            financeAd01.setAae140(aae140);
            financeAd01.setAae002(new BigDecimal(aae002));
            financeAd01.setAae805(null);
            financeAd01.setAaa039(aaa039);
            financeAd01.setAae710(aae710);
            financeAd01.setBae710(bae710);
            financeAd01.setAae719(aae719);
            financeAd01.setAaz617("");
            financeAd01.setAaz654(null);
            financeAd01.setAaz618(null);
            financeAd01.setAae072(aae072);
            financeAd01.setBae038("IC58");
            financeAd01.setAaa028(aaa028);
            financeAd01.setAaz010(new BigDecimal(aaz010));
            financeAd01.setAae734(aae734);
            financeAd01.setAab001(null);
            financeAd01.setAab004(aab004);
            financeAd01.setAab019(null);
            financeAd01.setAac058(aac58);
            financeAd01.setAac147(aac147);
            financeAd01.setAae718(aae718);
            financeAd01.setAaf200(aaf200);
            financeAd01.setAaf216(aaf216);
            financeAd01.setAaf217(aaf217);
            financeAd01.setAae668(aae668);
            financeAd01.setAae009(aae009);
            financeAd01.setAae010(aae010);
            financeAd01.setAab362("");
            financeAd01.setAaf056("");
            financeAd01.setAaf002(aaf002);
            financeAd01.setAad128(aad128);
            financeAd01.setAae711(aae711);
            financeAd01.setAae825(aae825);
            financeAd01.setBad006(bad006);
            financeAd01.setAae456(aae456);
            financeAd01.setAab191(null);
            financeAd01.setAae713(null);
            financeAd01.setAae151(null);
            financeAd01.setAae770(null);
            financeAd01.setBad011(null);
            financeAd01.setAaz728(null);
            financeAd01.setAaz749(aae072);
            financeAd01.setAad146(null);
            financeAd01.setAad080(aae140);
            financeAd01.setAae013("");
            financeAd01.setAae860(aae011);
            financeAd01.setAae859(new BigDecimal(aae036));
            financeAd01.setAae011(aae011);
            financeAd01.setAaz692(aaz692);
            financeAd01.setAae036(new BigDecimal(aae036));
            financeAd01.setAab034(null);
            financeAd01.setAab360(null);
            financeAd01.setAab359(aab359);
            financeAd01.setAaf018(null);
            financeAd01.setAaa027(null);
            financeAd01.setAaz002(new BigDecimal(aaz002));
            financeAd01.setAaz005(null);
            financeAd01.setAaa431(null);
            financeAd01.setBad112(null);
            financeAd01.setAaz670(null);
            financeAd01.setAae145(aae145);
            financeAd01.setAae006(null);
            financeAd01.setAae007(null);
            financeAd01.setBaz617(null);


            QueryWrapper<TransferIc58> wapper = new QueryWrapper();
            wapper.eq("aaz002", aaz002);
            List<Map<String, Object>> ic58 = transferIc58Service.listMaps(wapper);
            if (ic58.size() <= 0) {  //保存IC58 AD01

                transferIc58Service.save(transferIc58);
                financeAd01Service.save(financeAd01);

                MyMap MyMap = new MyMap<>();
                MyMap.putAll(businessParam);
                MyMap.put("bae038","IC58");
                MyMap.put("finish",true);
                transferProcessService.createProcessTransfer(MyMap);

            }


        }

        return new HashMap();
    }

    @Override
    public List<Map> querytransferoutBills(Map map) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("aaz002",map.get("aaz002"));
        return financeAd01Service.listMaps(queryWrapper);

    }

    /**
     * 转出人员支付表撤销
     * @param map
     * @return
     */
    @Override
    public Map transferoutCreatBillsRevoke( Map map) {
        String aaz002 = map.get("aaz002").toString();
        Map map1 = new HashMap<>();
        map1.put("aaz002",aaz002);
        transferIc58Service.removeByMap(map1);
        financeAd01Service.removeByMap(map1);

        return null;
    }

    //去掉空格和特殊空格 正常的空格ASCII值为32，特殊空格ASCII值为160
    public String deleteSpaceAndSpecialSpace(String info) {
        if (ValidateUtil.isNotEmpty(info)) {
            return info.replace(" ", "").replace("\t", "");
        } else {
            return info;
        }
    }
}
